define(['zepto', 'utils', 'JST'], function($, utils, JST){
  return {
  	init: function(_CTX_, sku) {
	    this._CTX_ = _CTX_
  	  this.sku = sku 
      this.model = {}
      var $id = $('[data-selected-coupon]')
      if ($id && $id.length) {
        this.model.selectedCouponId = parseInt($id.data('selected-coupon'))
      }
  	  this.$couponSelect = $('[href="#coupon-select"]')
  	  this.$couponSelect.length ? this.bindCouponBindingAction() : void 0

  	},

  	bindCouponBindingAction: function() {

  	  var that = this

  		this.$couponSelect.on('click', function(e) {
  			e.preventDefault()
  			var defer = that.compileCouponSelectPage()
        $.when(defer).done(function($page) {
          $.push.transitionIn($page)
        })

  		})

      // On selected
      this.$couponSelect.on('selected:activity:coupon', function(e, $page, id) {
        // Store selected round id
        that.model.selectedCouponId = id

        var coupon = that.getCoupon(id)

        // Transition out round selection page
        $.push.transitionOut($page, {
          beforeTransition: function() {
            that.$couponSelect.find('.item-inner').empty()
            that.$couponSelect.find('.item-inner').append(JST.couponItemTpl({'coupon': coupon}))
            that.$couponSelect.trigger('coupon:change:event')
          }
        })
      })

  	},

  	compileCouponSelectPage: function() {

  	  var that = this,
          deferPage = $.Deferred(),
          defer = this.requestCoupons()

      $.when(defer).done(function(data) {
        var $page = $(JST.couponSelectTpl({
            'coupons': data.coupons,
            'selectedCouponId': that.model.selectedCouponId
          }))

        // Store requested coupons
        that.model.coupons = data.coupons
        // Bind coupon page event
        that.bindCouponSelecttionPageEvent($page)

        deferPage.resolve($page)
      })

      return deferPage.promise()
  	},

    requestCoupons: function() {
      var defer = $.Deferred(),
          url = this._CTX_.root + '/shop/coupons?sku=' + this.sku

      // Show request indicator
      $.showIndicator()
      // Request for rounds data
      $.ajax({
        'type': 'GET',
        'url': url,
        'cache': false
      }).done(function(response) {
        $.hideIndicator()  // Hide request indicator
        utils.processResult(response, function(result) {
          defer.resolve(result.data)
        }, function(result) {
          $.toast(result.message || '获取优惠券失败')
        })
      })

      return defer.promise()
    },

    bindCouponSelecttionPageEvent:  function($page) {
      var that = this
      $page.on('change', 'input[name="coupon-radio"]', function(e) {
        var id = parseInt(e.currentTarget.value)
        that.$couponSelect.trigger('selected:activity:coupon', [$page, id])
      })
    },

    getCoupon: function(id) {
      return _.find(this.model.coupons, function(coupon) {
        if (coupon.id === id) {
          return coupon
        }
      })
    }
  }
})